package com.yangtao.prototype.system.log.web;

import java.util.Date;

import javax.servlet.http.HttpSession;

import com.yangtao.framework.constant.SysConstant;
import com.yangtao.framework.util.Struts2Utils;
import com.yangtao.framework.web.BaseAction;
import com.yangtao.prototype.system.entity.LogNote;
import com.yangtao.prototype.system.log.service.LogModel;
import com.yangtao.prototype.system.log.service.LogService;

public class LogAction extends BaseAction<LogModel, LogService> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// public String menu() throws Exception {
	// sysUser = (SysUser) request.getSession().getAttribute(
	// SysConstant.CURRENT_USER);
	// menus = service.getAhoritiedMenus(sysUser.getId(), null);
	// request.setAttribute("menus", menus);
	// // Struts2Utils.renderJson(menus);
	// return render("/layout.html");
	// // return render("/main.html");
	// }

	// public String subMenu() {
	// String topId = request.getParameter("topId");
	// menus = service.getAhoritiedMenus(sysUser.getId(), topId);
	// DojoDataModel ddm = new DojoDataModel(menus);
	// Struts2Utils.renderJson(ddm, true);
	// return null;
	// }

	public String login() throws Exception {
		// 验证用户是否合法，如果合法，进入登陆页面
		service.login(model);
		if (model.hasError()) {
			return "login";
		}
		// 将用户西信息保存在session中
		HttpSession session = request.getSession();
		session.setAttribute(SysConstant.CURRENT_USER, model.getDomain());
		// 增加登陆日志
		LogNote ln = new LogNote();
		ln.setUserId(model.getDomain().getId());
		ln.setUserName(model.getDomain().getUserName());
		ln.setClientIp(request.getRemoteAddr());
		ln.setClientAddress(request.getRemoteHost());
		ln.setLogInTime(new Date());
		ln.setState("在线");
		model.setLn(ln);
		service.addLogNote(model);
		session.setAttribute(SysConstant.LOG_ID, model.getLn().getId());
		return render("/layout.ftl");
		// return menu();
		// return render("/main.html");
	}

	/**
	 * 查看当前在线用户
	 */
	public String online() {
		service.getOnLineUser(model);
		return render("onlineUserList");
	}

	/**
	 * 登出
	 */
	public String logOut() {
		String logId = (String) request.getSession().getAttribute(
				SysConstant.LOG_ID);
		if (logId != null) {
			model.setLogId(logId);
			service.changeLogNote(model);
		}
		// 强制seesion失效
		request.getSession().invalidate();
		return "login";
	}

	/**
	 * 查询登陆日志
	 * 
	 * @return
	 */
	public String lognote() {
		return render("LogNoteQuery2");
	}

	/**
	 * 查询登陆日志列表
	 */
	public String lognoteList() {
		service.findLogs(model);
		Struts2Utils.renderJson(model.getPage());
		return null;
	}
}
